/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web.Dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import web.enity.UserInfo;
import web.Dao.UserDAO;
import web.util.DBUitl;

/**
 *
 * @author 10592
 */
public class UserDAOImpl implements UserDAO{

    @Override
    public int resiger(UserInfo user) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        int r = 0;
       
        PreparedStatement pst=null;
        try  (Connection con = DBUitl.getConnection()){
             pst = con.prepareStatement("insert into user (userName,password,email) VALUES (?,?,?)");//防止sql注入
            pst.setString(1, user.getUserName());
            pst.setString(2, user.getPassword());
            pst.setString(3, user.getEmail());
            r = pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
         }
        

        return r;
    }

    @Override
    public UserInfo login(String uname, String upwd) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        UserInfo user = null;
        
        ResultSet resultSet=null;
        PreparedStatement pst=null;
        try(Connection con = DBUitl.getConnection()){
            String sql = "select * from user where userName=? and password=?";
            pst = con.prepareStatement(sql);
            pst.setString(1, uname);
            pst.setString(2, upwd);
            resultSet = pst.executeQuery();
            if (resultSet.next()) {
                user = new UserInfo();
                user.setUserName((resultSet.getString(1)));
                user.setPassword( resultSet.getString(2));
                
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
       
    }

    @Override
    public boolean quary(String userName) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        boolean a=false;
        
        ResultSet resultSet=null;
        PreparedStatement pst=null;
        try (Connection con = DBUitl.getConnection()){
            String sql = "select * from user where userName=? ";
            pst = con.prepareStatement(sql);
            pst.setString(1, userName);
            resultSet = pst.executeQuery();
            if (resultSet.next()) 
                a=true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return a;
    }
    public static void main(String[] args) {
        UserDAO UserBiz = new UserDAOImpl();
        int r = 0;
        if (UserBiz.quary("111")) {
            UserInfo ui = UserBiz.login("111", "111");
            if (ui != null) {
                System.out.println("用户和密码正确，登录成功");
                r = 1;
            } else {
                r = 0;
                System.out.println("用户或密码错误，请重新输入");
            }
        } else {
            System.out.println("该用户不存在");
            r = -1;
        }
    }
}
